"""
外观磨损区间常量定义
"""
from dataclasses import dataclass
from typing import Dict, List
from enum import Enum, auto

@dataclass
class WearRange:
    """磨损区间"""
    show_name: str  # 显示名称
    name: str       # 名称
    max_val: float  # 最大值
    min_val: float  # 最小值

class WearCategory(Enum):
    """外观类型"""
    FACTORY_NEW = "WearCategory0"      # 崭新出厂 0.00-0.07
    MINIMAL_WEAR = "WearCategory1"      # 略有磨损 0.07-0.15
    FIELD_TESTED = "WearCategory2"      # 久经沙场 0.15-0.38
    WELL_WORN = "WearCategory3"         # 破损不堪 0.38-0.45
    BATTLE_SCARRED = "WearCategory4"    # 战痕累累 0.45-1.00

# 外观类型的中文名称映射
WEAR_CATEGORY_NAMES = {
    WearCategory.FACTORY_NEW: "崭新出厂",
    WearCategory.MINIMAL_WEAR: "略有磨损",
    WearCategory.FIELD_TESTED: "久经沙场",
    WearCategory.WELL_WORN: "破损不堪",
    WearCategory.BATTLE_SCARRED: "战痕累累",
}

# 每个外观类型的基础磨损区间
WEAR_CATEGORY_RANGES = {
    WearCategory.FACTORY_NEW: WearRange("0.00-0.07", "0.00-0.07", 0.07, 0.00),
    WearCategory.MINIMAL_WEAR: WearRange("0.07-0.15", "0.07-0.15", 0.15, 0.07),
    WearCategory.FIELD_TESTED: WearRange("0.15-0.38", "0.15-0.38", 0.38, 0.15),
    WearCategory.WELL_WORN: WearRange("0.38-0.45", "0.38-0.45", 0.45, 0.38),
    WearCategory.BATTLE_SCARRED: WearRange("0.45-1.00", "0.45-1.00", 1.00, 0.45),
}

# 崭新出厂的细分区间
FACTORY_NEW_SUBRANGES = [
    WearRange("0.00-0.01", "0.00-0.01", 0.01, 0.00),
    WearRange("0.01-0.02", "0.01-0.02", 0.02, 0.01),
    WearRange("0.02-0.03", "0.02-0.03", 0.03, 0.02),
    WearRange("0.03-0.04", "0.03-0.04", 0.04, 0.03),
    WearRange("0.04-0.07", "0.04-0.07", 0.07, 0.04),
]

# 略有磨损的细分区间
MINIMAL_WEAR_SUBRANGES = [
    WearRange("0.07-0.08", "0.07-0.08", 0.08, 0.07),
    WearRange("0.08-0.09", "0.08-0.09", 0.09, 0.08),
    WearRange("0.09-0.10", "0.09-0.10", 0.10, 0.09),
    WearRange("0.10-0.11", "0.10-0.11", 0.11, 0.10),
    WearRange("0.11-0.15", "0.11-0.15", 0.15, 0.11),
]

# 久经沙场的细分区间
FIELD_TESTED_SUBRANGES = [
    WearRange("0.15-0.18", "0.15-0.18", 0.18, 0.15),
    WearRange("0.18-0.21", "0.18-0.21", 0.21, 0.18),
    WearRange("0.21-0.24", "0.21-0.24", 0.24, 0.21),
    WearRange("0.24-0.27", "0.24-0.27", 0.27, 0.24),
    WearRange("0.27-0.38", "0.27-0.38", 0.38, 0.27),
]

# 破损不堪的细分区间
WELL_WORN_SUBRANGES = [
    WearRange("0.38-0.39", "0.38-0.39", 0.39, 0.38),
    WearRange("0.39-0.40", "0.39-0.40", 0.40, 0.39),
    WearRange("0.40-0.41", "0.40-0.41", 0.41, 0.40),
    WearRange("0.41-0.42", "0.41-0.42", 0.42, 0.41),
    WearRange("0.42-0.45", "0.42-0.45", 0.45, 0.42),
]

# 战痕累累的细分区间
BATTLE_SCARRED_SUBRANGES = [
    WearRange("0.45-0.50", "0.45-0.50", 0.50, 0.45),
    WearRange("0.50-0.63", "0.50-0.63", 0.63, 0.50),
    WearRange("0.63-0.76", "0.63-0.76", 0.76, 0.63),
    WearRange("0.76-0.90", "0.76-0.90", 0.90, 0.76),
    WearRange("0.90-1.00", "0.90-1.00", 1.00, 0.90),
]

def get_wear_ranges(category: WearCategory) -> List[WearRange]:
    """
    获取指定外观类型的磨损区间列表
    
    Args:
        category: 外观类型
        
    Returns:
        List[WearRange]: 磨损区间列表。对于崭新出厂、略有磨损、久经沙场、破损不堪和战痕累累返回细分区间
    """
    if category == WearCategory.BATTLE_SCARRED:
        return BATTLE_SCARRED_SUBRANGES
    elif category == WearCategory.FACTORY_NEW:
        return FACTORY_NEW_SUBRANGES
    elif category == WearCategory.MINIMAL_WEAR:
        return MINIMAL_WEAR_SUBRANGES
    elif category == WearCategory.FIELD_TESTED:
        return FIELD_TESTED_SUBRANGES
    elif category == WearCategory.WELL_WORN:
        return WELL_WORN_SUBRANGES
    raise ValueError(f"未知的外观类型: {category}")

def get_wear_range_by_name(category: WearCategory, range_name: str) -> WearRange:
    """
    根据区间名称获取磨损区间
    
    Args:
        category: 外观类型
        range_name: 区间名称
        
    Returns:
        WearRange: 磨损区间对象
        
    Raises:
        ValueError: 如果找不到指定的区间
    """
    ranges = get_wear_ranges(category)
    for wear_range in ranges:
        if wear_range.name == range_name:
            return wear_range
    raise ValueError(f"找不到区间: {range_name}")

def get_category_by_wear(wear: float) -> WearCategory:
    """
    根据磨损值获取对应的外观类型
    
    Args:
        wear: 磨损值
        
    Returns:
        WearCategory: 外观类型
        
    Raises:
        ValueError: 如果磨损值无效
    """
    if not 0 <= wear <= 1:
        raise ValueError(f"无效的磨损值: {wear}")
        
    for category, wear_range in WEAR_CATEGORY_RANGES.items():
        if wear_range.min_val <= wear <= wear_range.max_val:
            return category
            
    raise ValueError(f"找不到磨损值 {wear} 对应的外观类型")

def get_subrange_by_wear(category: WearCategory, wear: float) -> WearRange:
    """
    根据磨损值获取对应的细分区间
    
    Args:
        category: 外观类型
        wear: 磨损值
        
    Returns:
        WearRange: 磨损区间对象
        
    Raises:
        ValueError: 如果找不到对应的区间
    """
    ranges = get_wear_ranges(category)
    for wear_range in ranges:
        if wear_range.min_val <= wear <= wear_range.max_val:
            return wear_range
            
    raise ValueError(f"找不到磨损值 {wear} 对应的区间")

def get_category_name(category: WearCategory) -> str:
    """
    获取外观类型的中文名称
    
    Args:
        category: 外观类型
        
    Returns:
        str: 外观类型的中文名称
    """
    return WEAR_CATEGORY_NAMES[category]
